import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

import "home_page.dart";
import 'cart_page.dart';
import 'category_page.dart';
import 'member_page.dart';

class IndexPage extends StatefulWidget {
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {

  // 定义底部导航栏
  final List<BottomNavigationBarItem> bottimTabs = [
    BottomNavigationBarItem(
      icon: Icon(CupertinoIcons.home),
      title: Text("首页")
    ),
    BottomNavigationBarItem(
      icon: Icon(CupertinoIcons.search),
      title: Text("分类")
    ),
    BottomNavigationBarItem(
      icon: Icon(CupertinoIcons.shopping_cart),
      title: Text("购物车")
    ),
    BottomNavigationBarItem(
      icon: Icon(CupertinoIcons.profile_circled),
      title: Text("会员中心"),
    )
  ];

  // 将需要加载的页面引入
  final List tabBodies = [
    HomePage(),
    CategoryPage(),
    CartPage(),
    MemberPage()
  ];

  // 建立页面索引
  int currentIndex = 0;
  var currentPage;

  void initState() { 
    // 设置默认打开首页
    currentPage=tabBodies[currentIndex];
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color.fromRGBO(244, 245, 245, 1),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: currentIndex,
        items: bottimTabs,
        onTap: (index){
            setState(() {
             currentIndex=index;
             currentPage=tabBodies[index]; 
            });
        },
      ),
      body: currentPage,
    );
  }
}